package com.itc.bi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itc.bi.constant.ScreenConstant;
import com.itc.bi.entity.BigScreenPageVersionEntity;
import com.itc.bi.service.BigScreenPageVersionService;
import com.itc.bi.mapper.BigScreenPageVersionMapper;
import com.itc.bi.vo.bigScreenVO.ScreenVersionVO;
import com.itc.common.core.exception.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author hecai
* @description 针对表【big_screen_page_version(态势模板版本)】的数据库操作Service实现
* @createDate 2024-06-20 14:20:34
*/
@Service
public class BigScreenPageVersionServiceImpl extends ServiceImpl<BigScreenPageVersionMapper, BigScreenPageVersionEntity> implements BigScreenPageVersionService{

    /**
     * 根据code查询详情
     * @param code
     * @return
     */
    @Override
    public BigScreenPageVersionEntity getScreenVersionByCode(String code) {
        return this.getOne(Wrappers.<BigScreenPageVersionEntity>lambdaQuery()
                .eq(StringUtils.isNotEmpty(code),BigScreenPageVersionEntity::getCode, code));
    }

    /**
     * 更具上级目录取到唯一一个版本
     * @param parentCode
     * @return
     */
    @Override
    public BigScreenPageVersionEntity getScreenVersionByParentCode(String parentCode) {
        LambdaQueryWrapper<BigScreenPageVersionEntity> qw = new LambdaQueryWrapper<>();
        qw.eq(StringUtils.isNotEmpty(parentCode),BigScreenPageVersionEntity::getParentCode, parentCode);
        qw.orderByDesc(BigScreenPageVersionEntity::getUpdateTime);
        List<BigScreenPageVersionEntity> list = this.list(qw);
        if(CollectionUtil.isEmpty(list)){
            throw new ServiceException("未找到态势信息");
        }
        BigScreenPageVersionEntity versionEntity = list.stream().filter(p -> p.getIsPublish().equals(ScreenConstant.PUBLISH)).findFirst().orElse(null);
        if(ObjectUtil.isNotEmpty(versionEntity)){
            return versionEntity;
        }
        return list.get(0);
    }

    /**
     * 查询历史版本
     * @param parentCode
     * @return
     */
    @Override
    public List<BigScreenPageVersionEntity> getHistoryVerison(String parentCode){
        return this.list(Wrappers.<BigScreenPageVersionEntity>lambdaQuery()
                .select(BigScreenPageVersionEntity::getCode,BigScreenPageVersionEntity::getName,BigScreenPageVersionEntity::getCreateBy,BigScreenPageVersionEntity::getCreateTime,BigScreenPageVersionEntity::getIsPublish)
                .eq(StringUtils.isNotEmpty(parentCode),BigScreenPageVersionEntity::getParentCode, parentCode));
    }
}




